package com.github.wxpay.sdk;

import java.util.HashMap;
import java.util.Map;

public class MyWxPayUtil {
    /**
     *
     * @param name
     * @param ordersn
     * @param money 先不使用 money写死 固定1分钱
     * @return
     */
    public static String toPay(String name,String ordersn,Integer money){

        String responseXml = null;
        try {
            MyConfing myConfing = new MyConfing();
            HashMap<String, String> map = new HashMap<>();
            map.put("appid",myConfing.getAppID());
            map.put("mch_id",myConfing.getMchID());
            map.put("nonce_str",WXPayUtil.generateNonceStr());
            map.put("body",name);
            map.put("out_trade_no",ordersn);
            map.put("total_fee","1");
            map.put("spbill_create_ip","127.0.0.1");
            map.put("notify_url","http://hci8wvv.nat.ipyingshe.com/pay/notify.do");
            map.put("trade_type","NATIVE");


            String signedXml = WXPayUtil.generateSignedXml(map, myConfing.getKey());


            WXPayRequest wxPayRequest = new WXPayRequest(myConfing);


            responseXml = wxPayRequest.requestWithCert("/pay/unifiedorder", "", signedXml, true);


            Map<String, String> stringStringMap = WXPayUtil.xmlToMap(responseXml);


            System.out.println("获取到的支付链接是：-----" + stringStringMap.get("code_url"));
            return stringStringMap.get("code_url");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isSign(Map<String, String> map){
        MyConfing myConfing = new MyConfing();
        try {
            return WXPayUtil.isSignatureValid(map,myConfing.getKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
